import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/provider/user_provider.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:provider/provider.dart';

class SignaturePage extends StatefulWidget {
  SignaturePage({Key? key}) : super(key: key);

  @override
  _SignaturePageState createState() => _SignaturePageState();
}

class _SignaturePageState extends State<SignaturePage> {
  TextEditingController _signatureController = TextEditingController();
  final FocusNode _signatureNodeText = FocusNode();

  @override
  void initState() {
    super.initState();
    UserProvider provider = UserProvider();
    _signatureController.text = provider.user?.signature ?? '';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).setPersonalityIntroduction),
        centerTitle: true,
        elevation: 0.0,
      ),
      body: Consumer<UserProvider>(builder: (context, provider, _) {
        return SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: Dimens.gapDp15),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Gaps.vGap15,
                Row(
                  children: <Widget>[
                    Expanded(
                      child: Material(
                        borderRadius: BorderRadius.circular(Dimens.gapDp5),
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(
                            Dimens.gapDp15,
                            Dimens.gapDp5,
                            Dimens.gapDp15,
                            Dimens.gapDp15,
                          ),
                          child: TextField(
                            focusNode: _signatureNodeText,
                            maxLength: 50,
                            maxLines: 3,
                            autofocus: true,
                            controller: _signatureController,
                            textInputAction: TextInputAction.done,
                            keyboardType: TextInputType.text,
                            decoration: InputDecoration(
                              focusedBorder: InputBorder.none,
                              enabledBorder: InputBorder.none,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                Gaps.vGap20,
                ElevatedButton.icon(
                  icon: Icon(Icons.done),
                  label: Text(i18n(context).updates),
                  onPressed: () {
                    provider.updateUserInfo(
                        signature: _signatureController.text);
                    Navigator.maybePop(context);
                  },
                ),
              ],
            ),
          ),
        );
      }),
    );
  }
}
